package com.zx.util;

import java.util.ArrayList;
import java.util.List;

import com.zx.model.ConfContent;

public class ConfigNodeUtil {
	
	/**
	 * 根据配置节点的名称获得节点对象
	 * @param allConfContents
	 * @param nodeName
	 * @return
	 * @author: xue.zhang
	 * @date 2018年7月31日下午6:29:07
	 */
	public static List<ConfContent> getNode(List<ConfContent> allConfContents, String nodeName) {
		if(Assert.isEmpty(allConfContents)) {
			throw new RuntimeException("配置不能为空");
		} 

		List<ConfContent> list = new ArrayList<>();
		try {
			for (ConfContent confContent : allConfContents) {
				if(nodeName.equals(confContent.getConfName())) {
					list.add((ConfContent)confContent.clone());
					continue;
				}
				
				List<ConfContent> childContent = confContent.getChildContent();
				if(Assert.isNotEmpty(childContent)) {
					List<ConfContent> nodes = getNode(childContent, nodeName);//递归发现其他的配置
					if(Assert.isNotEmpty(nodes)) {
						list.addAll(nodes);
					}
				}
			}
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
}
